package source.controller;

import java.io.*;

/**
 * CustomKeyFile manager class is charged with file input and output operations
 * involved with the custom key bindings requirement of iteration 2.
 * @author Joe
 *
 */

public abstract class CustomKeyFileManager {

	private static final String defaultPath = "data/defaultKeyMap.txt";
	private static final String customPath = "data/customKeyMap";
	private static final int numKeys = 36;
	
	/**
	 * createPlayerKeyListener opens up a default file which is assumed to have
	 * all the default key bindings.  These bindings are mapped to a String array.
	 * Then the method searches for a specific player's custom file.  If one exists,
	 * the method replaces any default keybindings with all custom keybindings that
	 * are found.
	 * @param playerID: int
	 * @return
	 */
	public static PlayerKeyListener createPlayerKeyListener(int playerID)
	{
		String playerfilePath = customPath + playerID + ".txt";
		File defaultkeyFile = new File(defaultPath);
		File playerkeyFile = new File(playerfilePath);
		FileReader fr;
		BufferedReader br;
		String[] customKeyMap = new String[256];
		String[] defaultKeyMap = new String[256];
		for(int j = 0; j<256; j++)
		{
		  defaultKeyMap[j] = new String();
		  customKeyMap[j] = new String();
		}
		  
		/**
		 * Read the default file to populate the key 
		 */
		try
		{
		  fr = new FileReader(defaultkeyFile);
		  br = new BufferedReader(fr);
		  int index;
		  String line = new String();
		  String[] temp;

		  for(int i = 0; i<numKeys; i++)
		  {
	        if((line = br.readLine()) == null)
	        {
	    	    IOException e2 = new IOException("File is missing data");
	    	    throw e2;
	        }
	        else
	        {
	          temp = line.split(": ");
	          index = Integer.parseInt(temp[1]);
	          defaultKeyMap[index] = temp[0];
	          customKeyMap[index] = temp[0];
	        }
		  }
		}
		catch(FileNotFoundException e)
		{
			System.out.println(defaultPath + " not found");
			return null;
		}
		catch(IOException e)
		{
			System.out.println("IOException occured.");
			System.out.println(e.getMessage());
			return null;
		}
		
		/**
		 * Read the player's file to overwrite the default keys
		 */
		try
		{
		  playerkeyFile.createNewFile();
		  fr = new FileReader(playerkeyFile);
		  br = new BufferedReader(fr);
		  int index;
		  String line = new String();
		  String[] temp;

		  for(int i = 0; i<numKeys; i++)
		  {
	        if((line = br.readLine()) == null)
	        {
	    	    break;
	        }
	        else
	        {
	          temp = line.split(": ");
	          index = Integer.parseInt(temp[1]);
	          for(int k = 0; k<256; k++)
	          {
	        	  if(customKeyMap[k].equals(temp[0]))
	        	  {
	        		customKeyMap[k] = new String();
	        		break;
	        	  }
	          }
	          customKeyMap[index] = temp[0];
	        }
		  }
		  //System.out.println(buffer);
		  return new PlayerKeyListener(defaultKeyMap,customKeyMap, playerID);
		}
		catch(FileNotFoundException e)
		{
			System.out.println("File not found");
			return null;
		}
		catch(IOException e)
		{
			System.out.println("IOException occured.");
			System.out.println(e.getMessage());
			return null;
		}
	}
	/**
	 * writeToFile method takes a PlayerKeyListener and writes the current keybindings
	 * to file.  If the file doesn't exist, it is created.
	 * @param pkl: PlayerKeyListener
	 */
	public static void writeToFile(PlayerKeyListener pkl)
	{
		String[] keyMap = pkl.getKeyMap();
		String buffer = new String();
		for(int i = 0; i < 256; i++)
		{
			if(!keyMap[i].equals(new String()))
				buffer = buffer + keyMap[i] + ": " + i + "\n";
		}
		buffer = buffer.substring(0, buffer.length()-1);
			
		String filePath = customPath + pkl.getPlayerID() + ".txt";
		File keyFile = new File(filePath);
		FileWriter fw;
		BufferedWriter bw;
		try
		{
			keyFile.createNewFile();
			fw = new FileWriter(keyFile);
			bw = new BufferedWriter(fw);
			bw.write(buffer);
			bw.close();
			
		}
		catch(FileNotFoundException e)
		{
			System.out.println("File not found");
			return;
		}
		catch(IOException e)
		{
			System.out.println("IOException occured.");
			System.out.println(e.getMessage());
			return;
		}
			
		
		
		
	}
}
